home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mem / MPW Helpers / Procedures / filename.icn < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.6 KB  |  85 lines  |  [TEXT/MPS ]

  1. ############################################################################
  2. #
  3. #       Name:   filename.icn
  4. #
  5. #       Title:  Parse file names
  6. #
  7. #       Author: Robert J. Alexander
  8. #
  9. #       Date:   December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  suffix(s,separator) -- Parses a hierarchical file name, returning a
  14. #  2-element list:  [prefix,suffix].  E.g.  suffix("/a/b/c.d") ->
  15. #  ["/a/b/c","d"].  Separator is the character string that separates
  16. #  the suffix from the rest of the file name, and defaults to ".".
  17. #
  18. #  tail(s,separator) -- Parses a hierarchical file name, returning a
  19. #  2-element list:  [head,tail].  E.g.  tail("/a/b/c.d") ->
  20. #  ["/a/b","c.d"].  Separator is the character string that separates
  21. #  components of the file name, and defaults to the value of environment
  22. #  variable FILESEP, if defined, otherwise "/".
  23. #
  24. #  components(s,separator) -- Parses a hierarchical file name, returning
  25. #  a list of all directory names in the file path, with the file name
  26. #  (tail) as the last element.  E.g.  components("/a/b/c.d") ->
  27. #  ["/","a","b","c.d"].  Separator has the same meaning and default
  28. #  value as for tail().
  29. #
  30. #
  31. #  Setting the file name component separator
  32. #
  33. #  There are several ways to set the file name component separator.
  34. #  The normal way is to set up an environment variable on non-UNIX
  35. #  systems so the procedures will work correctly automatically (e.g.
  36. #  SET FILESEP=\ for MS-DOS, or Set FILESEP : ; Export FILESEP for
  37. #  MPW).  Other ways are:
  38. #
  39. #       -- Assign the separator string to global variable
  40. #          "FileSeparator"
  41. #       -- Provide a separator string in the second parameter
  42. #          of the procedure call.
  43. #
  44. ############################################################################
  45.  
  46. global FileSeparator
  47.  
  48. procedure suffix(s,separator)
  49.    local i
  50.    /separator := "."
  51.    i := *s + 1
  52.    every i := find(separator,s)
  53.    return [s[1:i],s[(*s >= i) + *separator:0] | &null]
  54. end
  55.  
  56. procedure tail(s,separator)
  57.    local i
  58.    initial set_separator()
  59.    /separator := FileSeparator
  60.    i := 0
  61.    every i := find(separator,s)
  62.    return case i of {
  63.      0: [,s]
  64.      1: [s[1+:*separator],s[1 + *separator:0]]
  65.      default: [s[1:i],s[i + *separator:0]]
  66.    }
  67. end
  68.  
  69. procedure components(s,separator)
  70.    local x,head
  71.    initial set_separator()
  72.    /separator := FileSeparator
  73.    x := tail(s,separator)
  74.    return case head := x[1] of {
  75.       separator: [separator]
  76.       &null: []
  77.       default: components(head,separator)
  78.       } ||| ([&null ~=== x[2]] | [])
  79. end
  80.  
  81. procedure set_separator()
  82.    initial /FileSeparator := getenv("FILESEP") | "/"
  83.    return
  84. end
  85.